package com.snakegame.ui;

import com.snakegame.core.GameManager;
import com.snakegame.entity.Food;
import com.snakegame.entity.Point;
import com.snakegame.entity.Snake;
import com.snakegame.config.Map;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List; // 导入 java.util.List

/**
 * 游戏面板，显示游戏的主要内容
 */
public class GamePanel extends JPanel {
    private GameManager gameManager;
    private GameFrame gameFrame; // 新增 GameFrame 引用
    private static final int CELL_SIZE = 20;

    public GamePanel(GameManager gameManager, GameFrame gameFrame) {
        this.gameManager = gameManager;
        this.gameFrame = gameFrame; // 初始化 GameFrame

        setPreferredSize(new Dimension(
            gameManager.getCurrentMap().getWidth() * CELL_SIZE,
            gameManager.getCurrentMap().getHeight() * CELL_SIZE
        ));
        setBackground(Color.BLACK);

        // 添加键盘监听器
        addKeyListener(new GameKeyListener(gameManager));
        setFocusable(true);
    }

    public GameFrame getGameFrame() {
        return gameFrame;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        // 绘制地图
        drawMap(g);
        
        // 绘制蛇
        drawSnakes(g);
        
        // 绘制食物
        drawFood(g);
        
        // 如果游戏暂停，显示暂停信息
        if (gameManager.isPaused()) {
            drawPauseMessage(g);
        }
    }
    
    private void drawMap(Graphics g) {
        Map map = gameManager.getCurrentMap();
        
        // 绘制网格
        g.setColor(Color.DARK_GRAY);
        for (int x = 0; x < map.getWidth(); x++) {
            for (int y = 0; y < map.getHeight(); y++) {
                g.drawRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
            }
        }
        
        // 绘制墙壁
        g.setColor(Color.GRAY);
        for (java.awt.Point wall : map.getWalls()) {
            g.fillRect(wall.x * CELL_SIZE, wall.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
        }
    }
    
    private void drawSnakes(Graphics g) {
        // 修改为 java.util.List
        List<Snake> snakes = gameManager.getSnakes();
        
        // 绘制第一条蛇（绿色）
        if (!snakes.isEmpty()) {
            Snake snake1 = snakes.get(0);
            g.setColor(Color.GREEN);
            
            // 绘制蛇身
            for (int i = 1; i < snake1.getBody().size(); i++) {
                Point p = snake1.getBody().get(i);
                g.fillRect(p.x * CELL_SIZE, p.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
            }
            
            // 绘制蛇头（深绿色）
            Point head = snake1.getHead();
            g.setColor(new Color(0, 100, 0));
            g.fillRect(head.x * CELL_SIZE, head.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
        }
        
        // 绘制第二条蛇（红色） - 双人模式
        if (snakes.size() >= 2) {
            Snake snake2 = snakes.get(1);
            g.setColor(Color.RED);
            
            // 绘制蛇身
            for (int i = 1; i < snake2.getBody().size(); i++) {
                Point p = snake2.getBody().get(i);
                g.fillRect(p.x * CELL_SIZE, p.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
            }
            
            // 绘制蛇头（深红色）
            Point head = snake2.getHead();
            g.setColor(new Color(139, 0, 0));
            g.fillRect(head.x * CELL_SIZE, head.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
        }
    }
    
    private void drawFood(Graphics g) {
        Food food = gameManager.getFood();
        if (food != null) {
            Point position = food.getPosition();
            g.setColor(Color.YELLOW);
            g.fillOval(position.x * CELL_SIZE, position.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
        }
    }
    
    private void drawPauseMessage(Graphics g) {
        g.setColor(Color.WHITE);
        g.setFont(new Font("宋体", Font.BOLD, 24));
        String message = "游戏暂停，按P继续";
        FontMetrics metrics = g.getFontMetrics();
        int x = (getWidth() - metrics.stringWidth(message)) / 2;
        int y = getHeight() / 2;
        g.drawString(message, x, y);
    }
}    